package com.shoulder.boot.ops.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shoulder.boot.ops.dto.MachineAddDTO;
import com.shoulder.boot.ops.dto.MachineModDTO;
import com.shoulder.boot.ops.vo.MachineVo;
import com.shoulder.boot.ops.dto.MachineQueryDTO;
import com.shoulder.boot.ops.entity.Machine;
import com.shoulder.boot.ops.service.MachineService;
import com.shoulder.boot.ops.vo.SourceVo;
import com.shoulder.core.api.ApiResult;
import com.shoulder.core.api.Kv;
import com.shoulder.log.annotation.LogAnnotation;
import com.shoulder.core.fetch.BaseIds;
import com.shoulder.core.page.PageSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;

import java.util.List;

/**
 * 机器信息管理
 */
@RestController
@AllArgsConstructor
@RequestMapping("/admin/api/v1/ops/machine")
public class MachineController {

    private final MachineService machineService;

    /**
     * 获取服务器资源
     * @param id
     * @return
     */
    @GetMapping("/source/{id}")
    public ApiResult<SourceVo> source(@PathVariable("id") String id) throws Exception {
        SourceVo result = machineService.source(id);
        return ApiResult.ok(result);
    }

    /**
     * 添加机器信息
     *
     * @param addDTO 机器信息添加请求体
     * @return ApiResult<Object>
     * @author unKnown
     */
    @PostMapping
    @LogAnnotation(type = "添加机器信息")
    public ApiResult<Object> add(@RequestBody @Validated MachineAddDTO addDTO) {
        this.machineService.add(addDTO);
        return ApiResult.ok();
    }

    /**
     * 修改机器信息
     *
     * @param modDTO 机器信息修改请求体
     * @return ApiResult<Object>
     * @author unKnown
     */
    @PutMapping
    @LogAnnotation(type = "修改机器信息")
    public ApiResult<Object> mod(@RequestBody @Validated MachineModDTO modDTO) {
        this.machineService.mod(modDTO);
        return ApiResult.ok();
    }

    /**
     * 查询机器信息分页数据
     *
     * @param queryDTO 机器信息查询请求体
     * @return 机器信息分页数据
     * @author unKnown
     */
    @LogAnnotation(type = "机器信息分页查询", record = false)
    @PostMapping("/page")
    public ApiResult<PageSchema<MachineVo>> pageQuery(@RequestBody MachineQueryDTO queryDTO) {
        Page<MachineVo> page = this.machineService.pageQuery(queryDTO);
        return ApiResult.ok(new PageSchema<>(page.getTotal(), page.getRecords()));
    }

    /**
     * 查询机器信息列表数据
     *
     * @param queryDTO 机器信息查询请求体
     * @return 机器信息列表数据
     * @author unKnown
     */
    @LogAnnotation(type = "机器信息列表查询", record = false)
    @PostMapping("/list")
    public ApiResult<List<MachineVo>> list(@RequestBody MachineQueryDTO queryDTO) {
        List<MachineVo> list = this.machineService.listQuery(queryDTO);
        return ApiResult.ok(list);
    }

    /**
     * 批量删除机器信息
     *
     * @param baseIds 机器信息id集合
     * @return ApiResult<Object>
     * @author unKnown
     */
    @PostMapping("/rm")
    @LogAnnotation(type = "批量删除机器信息")
    public ApiResult<Object> delete(@RequestBody @Validated BaseIds<String> baseIds) {
        this.machineService.batchDel(baseIds);
        return ApiResult.ok();
    }


    /**
     * 根据id删除机器信息
     *
     * @param id 机器信息id
     * @return ApiResult<Object>
     * @author unKnown
     */
    @DeleteMapping("/{id}")
    @LogAnnotation(type = "删除机器信息")
    public ApiResult<Object> del(@PathVariable String id) {
        this.machineService.del(id);
        return ApiResult.ok();
    }


    /**
     * 批量查询机器信息
     *
     * @param baseIds 机器信息id集合
     * @return 机器信息列表数据
     * @author unKnown
     */
    @PostMapping("/look")
    @LogAnnotation(type = "批量查询机器信息", record = false)
    public ApiResult<List<MachineVo>> looks(@RequestBody BaseIds<String> baseIds) {
        List<MachineVo> list = this.machineService.looks(baseIds);
        return ApiResult.ok(list);
    }

    /**
     * 根据id获取机器信息
     *
     * @param id 机器信息id
     * @return 机器信息
     * @author unKnown
     */
    @GetMapping("/{id}")
    @LogAnnotation(type = "根据id查询机器信息", record = false)
    public ApiResult<MachineVo> look(@PathVariable String id) {
        MachineVo look = this.machineService.look(id);
        return ApiResult.ok(look);
    }
}
